import enums.ResponseType;
import request.relation.FriendPullApplicationRequest;
import response.Response;
import response.relation.FriendPullApplicationResponse;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class FriendApplicationView extends JFrame {
    public int index;
    String hostUserName;
    String guestUserName;
    JScrollPane scrollPane = new JScrollPane();

    SpringLayout springLayout = new SpringLayout();
    JPanel panel = new JPanel(springLayout);

    DefaultListModel<String> listModel = new DefaultListModel<>();
    JList<String> jList;
    JButton agreeButton = new JButton("同意");
    JButton disagreeButton = new JButton("拒绝");


    public FriendApplicationView(String userName) {
        super("Friends Application");
        this.guestUserName = userName;
        jList = new JList<>();
        jList.addListSelectionListener(e -> {
            if (!jList.getValueIsAdjusting()) {    //设置只有释放鼠标时才触发
                index = jList.getSelectedIndex();
                hostUserName = (String) jList.getSelectedValue();
            }
        });
        scrollPane.setViewportView(jList);
        Font panelFont = new Font("楷体", Font.PLAIN, 15);
        agreeButton.setFont(panelFont);
        disagreeButton.setFont(panelFont);
        scrollPane.setFont(panelFont);
        agreeButton.addActionListener(new FriendApplicationHandler(this));
        disagreeButton.addActionListener(new FriendApplicationHandler(this));

        scrollPane.setPreferredSize(new Dimension(490, 480));
        panel.add(scrollPane);
        panel.add(agreeButton);
        panel.add(disagreeButton);

        springLayout.putConstraint(SpringLayout.NORTH, agreeButton, 20, SpringLayout.SOUTH, scrollPane);
        springLayout.putConstraint(SpringLayout.WEST, agreeButton, 100, SpringLayout.WEST, scrollPane);
        springLayout.putConstraint(SpringLayout.NORTH, disagreeButton, 0, SpringLayout.NORTH, agreeButton);
        springLayout.putConstraint(SpringLayout.WEST, disagreeButton, 100, SpringLayout.EAST, agreeButton);

        setSize(500, 600);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        getFriendApplications();
        getContentPane().add(panel);
    }

    public String getGuestUserName() {
        return this.guestUserName;
    }

    public String getHostUserName() {
        return this.hostUserName;
    }

    public void getFriendApplications() {
        FriendPullApplicationRequest friendPullApplicationRequest = new FriendPullApplicationRequest(guestUserName);
        Response response = ConnectionUtil.makeConnection(friendPullApplicationRequest);
        if (response.getType() == ResponseType.FriendPullApplication) {
            FriendPullApplicationResponse friendPullApplicationResponse = (FriendPullApplicationResponse) response;
            List<String> usernameList = friendPullApplicationResponse.getUsernameList();
            for (String username : usernameList) {
                listModel.addElement(username);
            }
            jList.setModel(listModel);
        }
    }
}
